﻿using System;

namespace _3
{
    // 定义数学工具类
    public class MyMath
    {
        // 1. 加法方法
        public double Add(double a, double b)
        {
            return a + b;
        }

        // 2. 减法方法
        public double Subtract(double a, double b)
        {
            return a - b;
        }

        // 3. 乘法方法
        public double Multiply(double a, double b)
        {
            return a * b;
        }

        // 4. 除法方法 - 添加除零检查
        public double Divide(double a, double b)
        {
            if (b == 0)
            {
                throw new DivideByZeroException("除数不能为零！");
            }
            return a / b;
        }

        // 5. 静态方法版本（可以直接通过类名调用）
        public static double StaticAdd(double a, double b) => a + b;
        public static double StaticSubtract(double a, double b) => a - b;
        public static double StaticMultiply(double a, double b) => a * b;

        public static double StaticDivide(double a, double b)
        {
            return b == 0 ? throw new DivideByZeroException("除数不能为零！") : a / b;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("=== MyMath 数学工具测试 ===\n");

            // 创建MyMath实例
            MyMath math = new MyMath();

            // 测试1：基本运算
            Console.WriteLine("1. 基本四则运算:");
            double a = 20.5;
            double b = 4.2;

            Console.WriteLine($"   {a} + {b} = {math.Add(a, b)}");
            Console.WriteLine($"   {a} - {b} = {math.Subtract(a, b)}");
            Console.WriteLine($"   {a} × {b} = {math.Multiply(a, b)}");
            Console.WriteLine($"   {a} ÷ {b} = {math.Divide(a, b):F2}"); // :F2 保留2位小数

            // 测试2：静态方法调用
            Console.WriteLine("\n2. 静态方法测试:");
            Console.WriteLine($"   静态加法: {MyMath.StaticAdd(10, 5)}");
            Console.WriteLine($"   静态除法: {MyMath.StaticDivide(100, 4)}");
            Console.WriteLine($"   静态减法: {MyMath.StaticSubtract(100, 4)}");
            Console.WriteLine($"   静态乘法: {MyMath.StaticMultiply(100, 4)}");
            // 测试3：用户交互测试
            Console.WriteLine("\n7. 用户交互测试:");
            Console.Write("   请输入第一个数字: ");
            double num1 = Convert.ToDouble(Console.ReadLine());

            Console.Write("   请输入第二个数字: ");
            double num2 = Convert.ToDouble(Console.ReadLine());

            Console.WriteLine($"\n   计算结果:");
            Console.WriteLine($"   {num1} + {num2} = {math.Add(num1, num2)}");
            Console.WriteLine($"   {num1} - {num2} = {math.Subtract(num1, num2)}");
            Console.WriteLine($"   {num1} × {num2} = {math.Multiply(num1, num2)}");
            Console.WriteLine($"   {num1} ÷ {num2} = {math.Divide(num1, num2):F4}");
        
            
               
            
         

          
        }
    }
}